﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace FennecUI
{
    public struct AppletConfiguration
    {
        public string Name;
        public Guid AppletID;
        public string FilePath;
        public AppletType Type;
        public string Icon;
        public List<String> FileHandlers;
        public string StartParameters;
        public string Description;
        public bool SingleInstance;
    
        public static AppletConfiguration FromXML(XmlNode appletXML){
            AppletConfiguration applet = new AppletConfiguration();
            applet.FileHandlers = new List<string>();
            applet.Name = appletXML["Name"].InnerText;
            applet.AppletID = Guid.ParseExact(appletXML["AppletID"].InnerText, "B");
            applet.FilePath = appletXML["FilePath"].InnerText;

            switch (appletXML["Type"].InnerText)
            {
                case "GenericNative":
                    applet.Type = AppletType.GenericNative;
                    break;
                case "NativeExtended":
                    applet.Type = AppletType.NativeExtended;
                    break;
                case "WebApplet":
                    applet.Type = AppletType.WebApplet;
                    break;
            }

            applet.Icon = appletXML["Icon"].InnerText;

            if (appletXML["FileHandlers"] != null)
            {
                foreach (XmlNode fileHandler in appletXML["FileHandlers"].ChildNodes)
                {
                    applet.FileHandlers.Add(fileHandler.InnerText);
                }
            }

            if (appletXML["StartParameters"]!=null)
            {
                applet.StartParameters = appletXML["StartParameters"].InnerText;
            }

            if (appletXML["Description"] != null)
            {
                applet.Description = appletXML["Decription"].InnerText;
            }

            if (appletXML["SingleInstance"] != null)
            {
                applet.SingleInstance = appletXML["SingleInstance"].InnerText == "true" ? true : false;
            }

            return applet;
        }
    }
    
    public enum AppletType:byte
    {
        GenericNative=0x01,
        NativeExtended=0x02,
        WebApplet=0x03
    }
}
